package server.core;

import server.Context;
import server.deploy.FilterDef;
import server.util.Enumerator;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.Enumeration;

/*
过滤器对象的包装类,同时实现FilterConfig接口
*/
/*
2023/2/27 该类内容全部写完,后续直接使用,无需修改
by cyl
 */
public final class ApplicationFilterConfig implements FilterConfig {
    //应用上下文(应用容器)
    private Context context = null;
    private FilterDef filterDef = null;
    private Filter filter = null;

    public ApplicationFilterConfig(Context context, FilterDef filterDef) throws ClassCastException, ClassNotFoundException,
            IllegalAccessException, InstantiationException, ServletException {
        this.context = context;
        setFilterDef(filterDef);
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer("ApplicationFilterConfig[");
        sb.append("name=");
        sb.append(filterDef.getFilterName());
        sb.append(", filterClass=");
        sb.append(filterDef.getFilterClass());
        sb.append("]");
        return sb.toString();
    }

    //销毁过滤器
    void release() {
        if (filter != null)
            filter.destroy();
        filter = null;
    }

    //getter--------------------------------------------------------------------------

    //如果filter实例已经存在,直接返回
    //否则加载filter类并创建实例,调用实例的init方法
    Filter getFilter() throws ClassCastException, ClassNotFoundException,
            IllegalAccessException, InstantiationException, ServletException {
        if (filter != null)
            return filter;
        String filterClass = filterDef.getFilterClass();
        ClassLoader classLoader = context.getLoader().getClassLoader();
        Class clazz = classLoader.loadClass(filterClass);
        filter = (Filter)clazz.newInstance();
        filter.init(this);
        return filter;
    }

    FilterDef getFilterDef() {
        return filterDef;
    }

    //setter--------------------------------------------------------------------------

    private void setFilterDef(FilterDef filterDef) throws ClassCastException, ClassNotFoundException,
            IllegalAccessException, InstantiationException, ServletException {
        this.filterDef = filterDef;
        if (filterDef == null) {
            if (filter != null)
                filter.destroy();
            filter = null;
        } else {
            getFilter();
        }
    }

    //实现FilterConfig接口--------------------------------------------------------------------------

    @Override
    public String getFilterName() {
        return filterDef.getFilterName();
    }

    @Override
    public String getInitParameter(String name) {
        return filterDef.getParameterMap().get(name);
    }

    @Override
    public Enumeration<String> getInitParameterNames() {
        return new Enumerator<String>(filterDef.getParameterMap().keySet());
    }

    @Override
    public ServletContext getServletContext() {
        return context.getServletContext();
    }
}
